//
//  PlanDetailController.m
//  XhlTrip
//
//  Created by dllo on 15/8/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PlanDetailController.h"
#import "LeaonWorking.h"
#import "PlanModel.h"
#import "planDetailHeader.h"
#import "PlanDetailCell.h"
#import "UMSocial.h"
#import "MBProgressHUD.h"

@interface PlanDetailController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *plaDayArr;
@property (nonatomic, retain) PlanDetailHeader *headerView;
@property (nonatomic, retain) NSMutableDictionary *resultDic;
@property (nonatomic, retain) NSString *headerText;

@property (nonatomic, retain) NSMutableArray *plan_nodes;
@end

@implementation PlanDetailController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.plaDayArr = [NSMutableArray array];
        self.resultDic = [NSMutableDictionary dictionary];
        
    }
    return self;
}


- (void)passValue:(NSString *)str planModel:(PlanModel *)mo nsstring:(NSString *)desText
{
    
    
    self.headerText = desText;
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSString *path = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/plans/", str, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"planDetail", str];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
        [self.resultDic setDictionary:result];
        [self.plaDayArr addObjectsFromArray:[result objectForKey:@"plan_days"]];
        
         self.plan_nodes = [NSMutableArray array];
        [self.plan_nodes addObjectsFromArray:[self getPlan_days_array:[self.resultDic objectForKey:@"plan_days"]]];
      
        /* headerView */
        self.headerView.planModel = mo;
        self.headerView.desLabel.text = desText;
        [_tableView reloadData];
        [hud removeFromSuperview];
        [hud release];
    }];
}

/* 数据整理 */
- (NSMutableArray *)getPlan_days_array:(NSMutableArray *)plan_days {
    NSMutableArray *newPlan_days = [NSMutableArray array];
    for (NSMutableDictionary *plan_day in plan_days) {
        plan_day = [self getPlan_day_dic:plan_day];
        [newPlan_days addObject:plan_day];
    }
    return newPlan_days;
}

- (NSMutableDictionary *)getPlan_day_dic:(NSMutableDictionary *)plan_day {
    NSMutableDictionary *newPlan_day = [NSMutableDictionary dictionary]; // 将要返回的字典
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithObject:[plan_day objectForKey:@"memo"] forKey:@"memo"]; // 新的小字典
    NSMutableArray *plan_nodes = [NSMutableArray arrayWithArray:[plan_day objectForKey:@"plan_nodes"]];
    [plan_nodes insertObject:tempDic atIndex:0];
    [newPlan_day setObject:plan_nodes forKey:@"plan_nodes"];
    return newPlan_day;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTabaleView];
    [self createShareButton];
    
}


- (void)createTabaleView
{
    CGFloat height = [PlanDetailHeader heightWithText:self.headerText width:self.view.frame.size.width - 20 * Width];
    self.headerView = [[[PlanDetailHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height + 195 * Height)] autorelease];
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    _tableView.rowHeight = 200 * Height;
   
    self.tableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableView];

    [_tableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlanDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"planDetail"];
    if (cell == nil) {
        cell = [[[PlanDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"planDetail"] autorelease];
    }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *dd = [[self.plan_nodes objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"];
    NSDictionary *ff = [dd objectAtIndex:indexPath.row];
    
    /* 给cell传值 */
    [cell.cellDic setDictionary:ff];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
    CGFloat mageHeight;
    
    NSArray *dd = [[self.plan_nodes objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"];
   
        
    
    NSDictionary *ff = [dd objectAtIndex:indexPath.row];
    
  
    if (![[ff allKeys] containsObject:@"image_url"] || [ff objectForKey:@"image_url"] == nil){
        
        mageHeight = 0;
    }else
    {
        mageHeight = 150 * Height;
    }

    if ([[ff allKeys] containsObject:@"memo"] && [ff objectForKey:@"memo"] != nil){
        height = [PlanDetailCell heightWithText:[ff objectForKey:@"memo"] width:self.view.frame.size.width - 20 * Width];
     
    }else if ([[ff allKeys] containsObject:@"tips"] && [ff objectForKey:@"tips"] != nil){
     height = [PlanDetailCell heightWithText:[ff objectForKey:@"tips"] width:self.view.frame.size.width - 20 * Width];
    }else
    {
        height = 0;
    }
        return height + mageHeight + 65 * Height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   /* 这样取更安全 */
    NSArray *dd = [[self.plan_nodes objectAtIndex:section] objectForKey:@"plan_nodes"];;
    return dd.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *arr = [self.plaDayArr[section] objectForKey:@"plan_nodes"];
    NSString *name = [[arr[0] objectForKey:@"destination"] objectForKey:@"name_zh_cn"];
    NSString *day = [NSString stringWithFormat:@"DAY%ld", section + 1];
    name = [NSString stringWithFormat:@"%@%@%@",day , @" ", name ];
    
    return name;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.plaDayArr.count;
}


- (void)createShareButton
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30 * Width, 30 * Height);
    [button addTarget:self action:@selector(shareButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[self nameWithPath:@"share.png"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    
    
}

/* 收藏点击事件 */
- (void)shareButton:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e3f7f967e58ee85b00110d"
                                      shareText:self.headerText
                                     shareImage:self.headerView.mage.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, UMShareToRenren, UMShareToDouban, UMShareToWechatSession,nil]
                                       delegate:self];
}

- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

- (void)dealloc
{
    [_plan_nodes release];
    [_headerView release];
    [_headerText release];
    [_resultDic release];
    [_plaDayArr release];
    [_tableView release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
